package com.hkws.hkws.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hkws.hkws.API.DoorResouceApi;
import com.hkws.hkws.mapper.DevlistMapper;
import com.hkws.hkws.pojo.AcsDeviceListRequest;
import com.hkws.hkws.pojo.Devlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;


/**
 * 查询门禁设备列表
 */
@Component
public class QueryDevList {
    @Autowired
    private DevlistMapper devlistMapper;
    private Object o;

  // @Scheduled(initialDelay = 100,fixedRate = 1000)
    public void execute() {
        DoorResouceApi doorResouceApi = new DoorResouceApi();
        AcsDeviceListRequest acsDeviceListRequest = new AcsDeviceListRequest();
        Devlist devList =   new Devlist();
        acsDeviceListRequest.setPageNo(1);
        acsDeviceListRequest.setPageSize(12);
        String s = doorResouceApi.acsDeviceList(acsDeviceListRequest);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");

        https://open-api.yunjiai.cn/v1/robot/info?accessKeyId=dhz6Ra7Jo6qLD6me&deviceId=896002959698169856&signature=6clyx6skjzQcmwHECbNd8H8zCS8%3D&signatureNonce=1&storeId=202189105671016556095847096576&timestamp=1";







        for(int i=0;i<list.size();i++) {
            JSONObject json1= (JSONObject) list.get(i);
            Field[] fields = devList.getClass().getDeclaredFields();
            for (Field field : fields) {
                // System.out.println(field.getName());
                Iterator<String> iterator = json1.keySet().iterator();
                while (iterator.hasNext()) {
                    String name = iterator.next();
                    if (field.getName().equals(name)) {
                        // System.out.println(name);
                        Object o = json1.get(name);
                        if (field.getType().toString().equals("class java.lang.String")) {
                            String o1 = o.toString();
                            System.out.println("o1" + o);
                            try {
                                field.setAccessible(true);
                                field.set(devList, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println("时间类型");
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                            Date ts = null;
                            try {
                                ts = sdf.parse((String) o);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            System.out.println(o);
                            try {
                                field.setAccessible(true);
                                field.set(devList, ts);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
            int insert = devlistMapper.insert(devList);
            if(insert==1)
            {
                System.out.println("插入成功");
            }
        }
    }
}
